/**
 * Author : duchuynh
 * Nov 9, 2011
 */
package wte.rnd.buildingontology.core;

import java.io.File;
import java.lang.reflect.Array;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.swing.JOptionPane;

import org.apache.axis.types.Entities;

import wte.rnd.buildingontology.core.EntityInfo;
import wte.rnd.buildingontology.gui.MainFrame;

import com.hp.hpl.jena.ontology.Ontology;
import com.hp.hpl.jena.util.FileUtils;

import edu.stanford.smi.protegex.owl.ProtegeOWL;
import edu.stanford.smi.protegex.owl.jena.JenaOWLModel;
import edu.stanford.smi.protegex.owl.model.OWLDatatypeProperty;
import edu.stanford.smi.protegex.owl.model.OWLIndividual;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
import edu.stanford.smi.protegex.owl.model.OWLObjectProperty;
import edu.stanford.smi.protegex.owl.model.RDFProperty;
import edu.stanford.smi.protegex.owl.model.RDFResource;
import edu.stanford.smi.protegex.owl.swrl.owlapi.OWLDataProperty;

public class OntologyModel {
	private JenaOWLModel owl;
	private String prefixClassName;
	
	public OntologyModel() {
		owl = null;
		prefixClassName = null;
	}
	
	public static void insertidInstanceToOWLModel(String nameInstance, String idInstance, String idParentClss, JenaOWLModel owlModel){
		try {
			OWLNamedClass parentClass = owlModel.getOWLNamedClass(idParentClss);
			OWLIndividual individual = parentClass.createOWLIndividual(idInstance.trim());
			individual.addLabel(nameInstance, "");
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
	}
	public static String queryNameOWLClassByNamed(String idClass, JenaOWLModel owlModel){
		OWLNamedClass clss;
		//if(this.prefixClassName != null){
		//	clss = this.owl.getOWLNamedClass(this.prefixClassName + ":" + idClass);
		//}else {
			clss = owlModel.getOWLNamedClass(idClass);	
		//}		
		if(clss != null){
			return clss.getLabels().toArray()[0].toString();
		}else {
			System.out.println("Can't get class with id :" + idClass + " from ontology");
			return null;
		}	
	}
	public static OWLNamedClass queryOWLClassByNamed(String idClass,JenaOWLModel owlModel){
		OWLNamedClass clss;
		//if(this.prefixClassName != null){
		//	clss = this.owl.getOWLNamedClass(this.prefixClassName + ":" + idClass);
		//}else {
			clss = owlModel.getOWLNamedClass(idClass);	
		//}
		if(clss != null){
			return clss;
		}else {
			System.out.println("Can't get class with id :" + idClass + " from ontology");
			return null;
		}
	}
	public static boolean saveModelToFileXML(String uriFile, JenaOWLModel owlModel){
		try {
			Collection errors = new ArrayList();
			owlModel.save(new File(uriFile).toURI(), FileUtils.langXMLAbbrev, errors);
			System.out.println("File saved with " + errors.size() + " errors.");
			return true;
		} catch (Exception e) {
			System.out.println("File not save : " + e.getMessage());
			return false;
		}
	}
	public static JenaOWLModel loadOntologyModel(String uriString){
		try {
			URI uri = new File(uriString).toURI();
			JenaOWLModel owl = ProtegeOWL.createJenaOWLModelFromURI(uri.toString());
			String prefix = owl.getDefaultOWLOntology().getLocalName();			
			//prefixClassName = (String) prefix.subSequence(0, prefix.indexOf("."));
			return owl;
		} catch (Exception e) {				
			System.out.println(e.getMessage());
			return null;
		}
	}
	public static ArrayList<EntityInfo> getListIndividualOfClass(String IDClass, JenaOWLModel owlModel){
		ArrayList<EntityInfo> listIndividual = new ArrayList<EntityInfo>();
		OWLNamedClass e1 = owlModel.getOWLNamedClass(IDClass );
		Collection instances = e1.getInstances(false);		
	    for (Iterator jt = instances.iterator(); jt.hasNext();) {
	        OWLIndividual individual = (OWLIndividual) jt.next();
	        EntityInfo a = new EntityInfo();
	        a.setClass(e1.getLabels().toArray()[0].toString());
	        a.setText(individual.getLabels().toArray()[0].toString());
	        a.setID(individual.getLocalName());
	        listIndividual.add(a);
	    }
		return listIndividual;
	}
	public static ArrayList<EntityInfo> searchIndividual(String name, JenaOWLModel owl){
		try {
			ArrayList<EntityInfo> list = new ArrayList<EntityInfo>();
			Collection instance = owl.getOWLIndividuals(false);
			for(Iterator ite = instance.iterator(); ite.hasNext();){
				OWLIndividual individual = (OWLIndividual) ite.next();
				if(individual.getLabels().toArray()[0].toString().equals(name)){
					String str = OntologyModel.queryNameOWLClassByNamed(individual.getRDFType().getBrowserText(), owl);
					EntityInfo entities = new EntityInfo();
					entities.setText(individual.getLabels().toArray()[0].toString());
					entities.setID(individual.getLocalName());					
					entities.setClass(str);
					list.add(entities);
				}				
			}	
			return list;
		} 
		catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Find false");
			return null;
		}
	}
	public static void main(String[] args) {
		JenaOWLModel owl = OntologyModel.loadOntologyModel("/media/DATA/Ontology Engineering/P2Ont/Ontology OWL/Onto.owl");
		OWLObjectProperty object = owl.getOWLObjectProperty("là_sản_phẩm_của");
		Collection collection = object.getDomains(false);
		for (Iterator it = collection.iterator(); it.hasNext();) {
			OWLNamedClass namedClass = (OWLNamedClass) it.next();
			System.out.println(namedClass.getBrowserText());
		}
		
		
		
				/*Collection collection = owl.getRDFProperties();
		for(Iterator it = collection.iterator(); it.hasNext();){
			RDFProperty property = (RDFProperty) it.next();
			if(!property.isSystem()){
				property.getDomains(false);
				System.out.println(property.getLabels().toArray()[0].toString());
			}			
		}*/
		
		
		/*ArrayList<EntityInfo> list = OntologyModel.searchIndividual("Sirius", owl);		
		for (EntityInfo entityInfo : list) {
			System.out.println(entityInfo.getEntityClass());
		}*/
	}
}

